﻿<?php
class MessageModel extends Model{

	public $id;
	public $date;
	public $login;
	public $text;
	
	public function __construct()
	{
	}
	
	public static function GetLatest($init = false)
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$q =   "SELECT * FROM message WHERE id > ".$_SESSION['last_message_id']." ORDER BY `date` ASC";

		$q=$connec->query($q);
		$q->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'MessageModel'); 
		$results = $q->fetchAll();
		
		if (count($results) > 0)			
			$_SESSION['last_message_id'] = $results[count($results)-1]->id;
		
		return $results;
	}
	
	public static function GetLastId()
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$q =   "SELECT MAX(id) FROM message";
		$q=$connec->query($q);
		$q = $q->fetch();
		
		return ($q[0] != null) ? $q[0] : 0;
	}
}
?>
